package lex.dm.ui.dbtree;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.Icon;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import org.lex.swing.ActionBuilder;

import lex.dm.DTree;
import lex.dm.utils.Resources;

public class DatabaseTree extends JTree
{
	public DatabaseTree()
	{
		super( new DBNode( -1, "", false ) );
		this.setRootVisible( true );
//		this.setToggleClickCount( 2 );
		this.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
		this.setCellRenderer( new DBNodeRenderer() );
		this.setComponentPopupMenu( new TablePopup() );
	}

	public void setConnected( boolean b )
	{
		if( b )
		{
			this.reload();
		}
		else
		{
			this.setModel( new DefaultTreeModel( new DBNode( -1, "", false ) ) );
		}
	}

	private void reload()
	{
		try
		{
			this.setModel( new DefaultTreeModel( DBTreeUtils.createRootNode( DTree.getApplication()
				.getConnection() ) ) );
		}
		catch( SQLException e )
		{
			e.printStackTrace();
		}
	}

	private class TablePopup extends JPopupMenu implements ActionListener
	{
		private ActionBuilder view = null;

		public TablePopup()
		{
			view = new ActionBuilder( "View" );
			view.setActionListener( this );
			this.add( view );
			this.addSeparator();
			this.add( new ActionBuilder( "Reload" ).setActionListener( new ActionListener(){
				public void actionPerformed( ActionEvent e )
				{
					reload();
				}
			} ) );
		}

		public void actionPerformed( ActionEvent e )
		{
			DBNode node = this.getSelectedNode();
			if( null == node || DBNode.TABLE_NODE != node.getNodeType() )
				return;
			String table = (String) node.getUserObject();
			DTree.getApplication().getContentPane().execute(  "select * from " + table );
		}

		@Override
		public void show( Component invoker, int x, int y )
		{
			DBNode node = this.getSelectedNode();
			if( null == node || DBNode.TABLE_NODE != node.getNodeType() )
				view.setEnabled( false );
			else
				view.setEnabled( true );
			super.show( invoker, x, y );
		}

		private DBNode getSelectedNode()
		{
			TreePath path = DatabaseTree.this.getSelectionPath();
			if( null == path )
				return null;
			DBNode node = (DBNode) path.getLastPathComponent();
			if( null == node )
				return null;
			return node;
		}
	}

	private static class DBNodeRenderer extends DefaultTreeCellRenderer
	{
		private static Icon CATALOG = Resources.getIcon( Resources.CATALOG_ICON );
		private static Icon TABLE = Resources.getIcon( Resources.TABLE_ICON );
		private static Icon FIELD = Resources.getIcon( Resources.FIELD_ICON );
		private static Icon TYPE = Resources.getIcon( Resources.TYPE_ICON );

		@Override
		public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel,
			boolean expanded, boolean leaf, int row, boolean hasFocus )
		{
			super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasFocus );
			this.setIcon( this.getIcon( ((DBNode) value).getNodeType() ) );
			return this;
		}

		private Icon getIcon( int nodeType )
		{
			switch( nodeType )
			{
			case DBNode.CATALOG_NODE:
				return CATALOG;
			case DBNode.TABLE_NODE:
				return TABLE;
			case DBNode.FIELD_NODE:
				return FIELD;
			case DBNode.TYPE_NODE:
				return TYPE;
			default:
				return null;
			}
		}
	}
}
